//
//  YQLHttpHandler.m
//  SJZBusiness
//
//  Created by 位立志 on 2018/7/16.
//  Copyright © 2018年 bwton. All rights reserved.
//

#import "YQLHttpHandler.h"
#import "HTTPRequest.h"
#import "SJZCouponModel.h"

@implementation YQLHttpHandler


+ (void)sendCodeWithPhone:(NSString *)phone
               completion:(void (^)(BOOL isSuccess, NSString *errStr)) completion{
    
    if (phone.length == 0) {
        [MBProgressHUD showErrorMessage:@"手机号不能为空"];
        return;
    }
    
    NSDictionary *params = @{
                             @"phone":phone
                             };
    
    [HTTPRequest sendCodeWithParames:params httpRequestBlock:^(id response, NSString *codeString, NSString *errorString) {
        if (!errorString) {
            completion(YES, nil);
        }else{
            completion(NO, errorString);
        }
    }];
}

+ (void)loginWithPhone:(NSString *)phone
                  code:(NSString *)code
            completion:(void (^)(BOOL isSuccess, NSString *errStr, SJZAccountModel *account)) completion{
    
    if (phone.length == 0) {
        [MBProgressHUD showErrorMessage:@"手机号不能为空"];
        return;
    }
    
    if (code.length == 0) {
        [MBProgressHUD showErrorMessage:@"验证码不能为空"];
        return;
    }
    
    NSDictionary *params = @{
                             @"phone":phone,
                             @"code":code
                             };
    
    [HTTPRequest loginWithParames:params httpRequestBlock:^(id response, NSString *codeString, NSString *errorString) {
        if (!errorString) {
            
            SJZAccountModel *accountModel = [SJZAccountModel mj_objectWithKeyValues:response];
            
            completion(YES, nil,accountModel);
        }else{
            completion(NO, errorString,nil);
        }
    }];
    
}

+ (void)listConsumeVoucherWithuserId:(NSString *)userId
                                page:(NSString *)page
                              length:(NSString *)length
                         voucherCode:(NSString *)voucherCode
                          completion:(void (^)(NSArray *voucherArray, NSString *errStr)) completion{
    
    NSDictionary *params = @{
                             @"userId":userId,
                             @"page":page,
                             @"length":length,
                             @"voucherCode":voucherCode
                             };
    
    [HTTPRequest listConsumeVoucherWithParames:params httpRequestBlock:^(id responseData, NSString *codeString, NSString *errorString) {
        if (!errorString) {
            NSArray *couponArray = [SJZCouponModel mj_objectArrayWithKeyValuesArray:responseData];
            completion(couponArray,nil);
        }else{
            completion(nil,errorString);
        }
    }];
    
}

+ (void)listStatConsumeVoucherWithuserId:(NSString *)userId
                              completion:(void (^)(NSArray *dictArray,NSString *dateRange,NSString *totalCount, NSString *errStr)) completion{
    
    NSDictionary *params = @{
                             @"userId":userId
                             };
    [HTTPRequest listStatConsumeVoucherWithParames:params httpRequestBlock:^(id responseData, NSString *codeString, NSString *errorString) {
        if (!errorString) {
            if (responseData) {
                NSString *dateRange = responseData[@"dateRange"];
                NSString *totalCount = responseData[@"totalCount"];
                if ([responseData[@"voucherStats"] count]) {
                    NSArray *dictArray = [NSArray arrayWithArray:responseData[@"voucherStats"]];
                    completion(dictArray,dateRange,totalCount,nil);
                }else{
                    completion(nil,dateRange,totalCount,nil);
                }
                
            }else{
                completion(nil,nil,nil,nil);
            }

        }else{
            completion(nil,nil,nil,errorString);
        }
    }];
    
}


+ (void)consumeVoucherWithuserId:(NSString *)userId
                     voucherCode:(NSString *)voucherCode
                      completion:(void (^)(SJZCouponModel *couponModel, NSString *errStr)) completion{
    
    NSDictionary *params = @{
                             @"userId":userId,
                             @"voucherCode":voucherCode
                             };
    
    [HTTPRequest consumeVoucherWithParames:params httpRequestBlock:^(id response, NSString *codeString, NSString *errorString) {
        if (!errorString) {
            SJZCouponModel *couponModel = [SJZCouponModel mj_objectWithKeyValues:response];
            completion(couponModel, nil);
        }else{
            completion(nil, errorString);
        }
    }];
    
}




@end
